package cn.tign.hz.run;

import cn.tign.hz.comm.DeclareDetails;
import cn.tign.hz.exception.DefineException;
import cn.tign.hz.helper.*;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 宫清
 * @description 签署流程测试启动
 * @date 2019年7月21日 下午9:38:31
 * @since JDK1.7
 */
public class OneStepStart {
    private static final Logger LOGGER = LoggerFactory.getLogger(OneStepStart.class);

    static {
        // 声明条款
        DeclareDetails.showImportantMessage();
    }

    // -------------------------------------------公有方法start-------------------------------------------------------

    public static void main(String[] args) {

        try {
            String filePath = "d:/test/123.pdf";

            LOGGER.info("---------------------获取token start------------------------------");
            TokenHelper.getTokenData();

            LOGGER.info("---------------------创建个人账号start-------------------------------");
            //生成签名
            JSONObject personAcctJson = AccountHelper.createPersonAcct("DH0008811", "董浩", "CRED_PSN_CH_IDCARD", "412801198908282613", "18171293809", null);
            String acctId = personAcctJson.getString("accountId");
            LOGGER.info("---------------------acctId1-------------------------------" + acctId);

            JSONObject copierAcctJson = AccountHelper.createPersonAcct("wjj101", "魏家骏", "CRED_PSN_CH_IDCARD", "420117199607056732", "18162681297", null);
            String copierAcctId = copierAcctJson.getString("accountId");
            LOGGER.info("---------------------acctId2-------------------------------" + copierAcctId);
            LOGGER.info("---------------------创建个人账号end-------------------------------");

            LOGGER.info("---------------------创建个人印章----------------------------------");
            JSONObject orgAcctJson = SealHelper.createPersonTemplateSeal(acctId, null, "RED", 95, 95, "RECTANGLE");
            String url1 = orgAcctJson.getString("url");
            String sealId1 = orgAcctJson.getString("sealId");
            String fileKey1 = orgAcctJson.getString("fileKey");
            LOGGER.info("-----------" + orgAcctJson.toString());
            JSONObject orgAcctJson2 = SealHelper.createPersonTemplateSeal(copierAcctId, null, "RED", 95, 95, "RECTANGLE");
            String url2 = orgAcctJson2.getString("url");
            String sealId2 = orgAcctJson2.getString("sealId");
            String fileKey2 = orgAcctJson2.getString("fileKey");
            LOGGER.info("-----------" + orgAcctJson2.toString());

            LOGGER.info("---------------------创建机构账号start----------------------------------");
//			JSONObject orgAcctJson = AccountHelper.createOrgAcct("BBBB", acctId, "天之云信息科技有限公司", null, "52227058XT51M4AL62");
//			String orgId = orgAcctJson.getString("orgId");

            LOGGER.info("---------------------创建机构印章start----------------------------------");
//			JSONObject orgSealJson = SealHelper.createOrgTemplateSeal(orgId, "天之云公司印章", "BLUE", null, null, null, null, "TEMPLATE_ROUND", "STAR");
//			String orgSealId = orgSealJson.getString("sealId");
//			
            LOGGER.info("---------------------通过上传方式创建文件start-----------------------------");
            String fileName = "测试董浩的合同.pdf";
            JSONObject uploadJson = FileTemplateHelper.createFileByUpload(filePath, fileName, acctId);
            String uploadUrl = uploadJson.getString("uploadUrl");
            String fileId = uploadJson.getString("fileId");
            LOGGER.info("------上传文档------" + uploadJson.toString());

            LOGGER.info("---------------------文件流上传文件start---------------------------------");
            FileTemplateHelper.streamUpload(filePath, uploadUrl);

            String[] usrs = new String[]{acctId, copierAcctId};
            LOGGER.info("---------------------一步发起签署start---------------------------------");
            JSONObject flowJson = SignHelper.oneStepFlow(copierAcctId, fileId, fileName, usrs, 0);
            String flowId = flowJson.getString("flowId");

            LOGGER.info("---------------------签署流程开启 start-----------------------------");
            SignHelper.startSignFlow(flowId);
            LOGGER.info("---------------------flowId-----------------------------" + flowId);

//			LOGGER.info("---------------------签署完成后，通知回调，平台方进行签署流程归档 start-----------------------------");
//			SignHelper.archiveSignFlow(flowId);

//			LOGGER.info("---------------------归档后，获取文件下载地址 start-----------------------------");
//			SignHelper.downloadFlowDoc(flowId);

        } catch (DefineException e) {
            e.getE().printStackTrace();
        }

    }
}
